package jsf;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import util.SessionUtil;
import ejb.ClassesBeanRemote;
import ejb.SemestersBeanRemote;
import ejb.StudentsBeanRemote;
import ejb.SubjectsBeanRemote;
import entity.Classes;
import entity.Semesters;
import entity.Students;
import entity.Subjects;

@SessionScoped
@ManagedBean
public class ClassesController implements Serializable {

	private static final long serialVersionUID = 1L;
	@EJB(lookup="java:global/EnrollmentManagementEAP/EnrollmentManagementEJB/ClassesBean!ejb.ClassesBeanRemote")
	ClassesBeanRemote bean;
	@EJB(lookup="java:global/EnrollmentManagementEAP/EnrollmentManagementEJB/StudentsBean!ejb.StudentsBeanRemote")
	StudentsBeanRemote studentsBean;
	@EJB(lookup="java:global/EnrollmentManagementEAP/EnrollmentManagementEJB/SemestersBean!ejb.SemestersBeanRemote")
	SemestersBeanRemote semestersBean;
	@EJB(lookup="java:global/EnrollmentManagementEAP/EnrollmentManagementEJB/SubjectsBean!ejb.SubjectsBeanRemote")
	SubjectsBeanRemote subjectsBean;
	private Long selectedId;
	private Classes clazz = new Classes();
	private String errorMsg;
	private String studentId;
	private ArrayList<Classes> classes = new ArrayList<Classes>();
	private ArrayList<Semesters> semesterList = new ArrayList<Semesters>();
	private ArrayList<Subjects> subjectList = new ArrayList<Subjects>();
	
	public ClassesController() {
		SessionUtil.authenticate();
	}
	
	// For Index Form
	public ArrayList<Classes> getClasses() {
		ArrayList<Classes> classes = new ArrayList<Classes>();
		classes.addAll(bean.getAll());
		this.classes = classes;
		return this.classes;
	}
	
	// For Edit Form
	public ArrayList<Semesters> getSemesterList() {
		ArrayList<Semesters> semesters = new ArrayList<Semesters>();
		semesters.addAll(semestersBean.getAll());
		this.semesterList = semesters;
		return semesterList;
	}
	
	// For Edit Form
	public ArrayList<Subjects> getSubjectList() {
		ArrayList<Subjects> subjects = new ArrayList<Subjects>();
		subjects.addAll(subjectsBean.getAll());
		this.subjectList = subjects;
		return subjectList;
	}
	
	// For Edit Form
	public Classes getClazz() {
		return this.clazz;
	}
	
	// For Edit Form
	public String getErrorMsg() {
		return errorMsg;
	}
	
	// For Edit Form
	public String getStudentId() {
		return studentId;
	}
	
	// For Edit Form
	public void setStudentId(String studentId) {
		this.studentId = studentId;
	}
	
	// For Edit, Enroll Form
	private Long getSelectedId() {
        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
        return Long.parseLong(context.getRequestParameterMap().get("selectedId"));
    }
	
	// For Index Form
	public String enroll() {
        selectedId = this.getSelectedId();
        if(selectedId != null) {
            this.clazz = this.bean.get(selectedId);
            return "enroll.xhtml";
        }
        return "classes.xhtml";
    }
	
	// For Index Form
	public String edit() {
        selectedId = this.getSelectedId();
        if(selectedId != null) {
            this.clazz = this.bean.get(selectedId);
            return "class.xhtml";
        }
        return "classes.xhtml";
    }
	
	// For Index Form
	public String create() {
		this.selectedId = null;
        this.clazz = new Classes();
        return "class.xhtml";
    }
	
	// For Edit Form
	public String doSubmit() {
        if(selectedId == null) {
            return this.doCreate();
        } else {
            return this.doEdit();
        }
    }
	
	// For Edit Form
	public String doEdit() {
        this.bean.update(this.clazz);
        return "classes.xhtml";
    }
    
	// For Edit Form
    public String doCreate() {
    	this.bean.insert(this.clazz);
        return "classes.xhtml";
    }
	
    // For Index Form
	public void doDelete() {
        Long selectedId = this.getSelectedId();
        Classes selectedClazz = this.bean.get(selectedId);
        if(selectedClazz != null) {
            this.bean.delete(selectedClazz);
        }
    }
	
	// For Enroll Form
	public void doAddStudent() {
		if(this.validate()) {
			Students student = studentsBean.getByStudentId(this.studentId);
			this.clazz.addStudent(student);
			this.bean.update(this.clazz);
		}
	}
	
	// For Enroll Form
	private boolean validate() {
		List<Students> students = this.clazz.getStudents();
		Students selected = studentsBean.getByStudentId(this.studentId);
		
		// Validate for existing student in the system
		if(selected == null) {
			this.errorMsg = "Error. The student that has Student ID = '"+ this.studentId +"' is not exist in the system.";
			return false;
		}
		
		// Validate for violating the max students in class
		if(students.size() == this.clazz.getMaxStudents()) {
			this.errorMsg = "Error. Can not add more student because the class is full.";
			return false;
		}
		
		// Validate for duplicating student in class
		for(int i = 0; i < students.size(); i++) {
			Students student = students.get(i);
			if(this.studentId.equals(student.getStudentId())) {
				this.errorMsg = "Error. The student that has Student ID = '"+ this.studentId +"' is already in class.";
				return false;
			}
		}
		
		return true;
	}

}
